import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 98152
 * Date: 2024-11-02
 * Time: 14:30
 */
public class Test2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] prices = new int[n];
        for(int i = 0; i < n; i++) {
            prices[i] = in.nextInt();
        }
        int cost = 0;
        int revenue = 0;
        boolean flag = true;
        for(int i = 0; i < n-1; i++) {
            if(flag && prices[i] < prices[i+1]) {
                cost = prices[i];
                flag = false;
            }
            if(!flag && prices[i] > prices[i+1]) {
                revenue += prices[i]-cost;
                flag = true;
            }
        }
        if(!flag) {
            revenue += prices[n-1]-cost;
        }
        System.out.println(revenue);
    }
}
